package com.scidpro.util;

import java.util.ArrayList;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

public class MyDb {

	public static void putLob(Context context, String json) {
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
		Editor e = settings.edit();
		e.putString("raw_lob", json);
		e.commit();
	}

	public static String getLobAsJson(Context context) {
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
		String result = settings.getString("raw_lob", null);
		return result;
	}

	public static ArrayList<Book> getLob(Context context) {
		ArrayList<Book> result = BooksJsonParser.convertJsonToBooksList(getLobAsJson(context));
		return result;
	}

	public static Book getBook(Context context, String id) {
		if (id == null) {
			return null;
		}
		ArrayList<Book> books = BooksJsonParser.convertJsonToBooksList(getLobAsJson(context));
		for (Book book : books) {
			if (book.id.equals(id) || id.endsWith(book.id)) {
				return book;
			}
		}
		return null;
	}

}
